import router from '@system.router';
import prompt from '@system.prompt';
import http from '@ohos.net.http';

export default {
    data: {
        name: "types page",
        activeTab: 0,
        tabName: [
            "Xiaomi手机",
            "Redmi手机",
            "游戏手机",
            "电脑平板",
            "智能穿戴",
            "电视",
            "大家电",
            "小家电"
        ],
        commodity: [
            {
                itemId: 0,
                itemName: "",
                itemDescription: "",
                itemImagePath: "",
                itemPrice: "",
            }
        ],
    },
    onInit() {
        this.getListData()
    },
    tabChange(e) {
        this.activeTab = e.index
        console.info("Tab index: " + this.activeTab)
        this.getListData()
    },
    getListData() {
        let httpRequest = http.createHttp();
        httpRequest.on('headerReceive', (err, data) => {
            if (!err) {
                console.info('header: ' + data.header);
            } else {
                console.info('error:' + err.data);
            }
        });
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
        this.$app.$def.getItemSearchUrl(),
            {
                method: 'GET', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json'
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {
                    searchInfo: this.tabName[this.activeTab]
                }
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                var result = JSON.parse(data.result)
                if (result.code == 1) {
                    // 获取成功
                    this.commodity = result.info
                    console.info(this.name+" goods:"+this.commodity)
                } else {
                    // 获取失败
                    this.commodity = []
                    console.info(this.name + ' fail:' + result.msg);
                    prompt.showToast({
                        message: result.msg
                    })
                }
            } else {
                console.info(this.name + ' error:' + err.data);
                prompt.showToast({
                    message: err.data
                })
            }
        });
    },
    goodsOnclick(i) {
        router.push({
            uri: "pages/detail/detail",
            params:{
                commodity:this.commodity[i]
            }
        })
    },
    search() {
        console.log("触发了搜索")
        console.log(this.searchInfo)
        router.push({
            uri: "pages/searchlist/searchlist",
            params:{searchInfo:this.searchInfo}
        })
    },
    searchinfo(event) {
        this.searchInfo = event.value;
        console.log(this.searchInfo);
    }
}
